package cn.yangliu.jwt.web;

import cn.yangliu.jwt.comm.JsonResult;
import cn.yangliu.jwt.entity.Role;
import cn.yangliu.jwt.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

/**
 * @author 杨柳
 * @contact QQ: 574789490
 * @date 2019-04-18 10:40
 */
@Api(value = "Role操作接口", tags = "Role操作接口", description = "Role操作接口")
@RequestMapping("/role")
@RestController
@Slf4j
public class RoleController {

    @Autowired
    private RoleService roleService;


    /**
     * 新增或修改
     */
    @PostMapping
    @ApiOperation(value = "新增或修改Role", httpMethod = "POST", notes = "新增或修改Role", tags = "Role操作接口", response = JsonResult.class)
    public JsonResult save(@RequestBody Role data) {
        roleService.save(data);

        return JsonResult.getInstance();
    }

    /**
     * 列表查询
     */
    @GetMapping
    @ApiOperation(value = "列表查询Role", httpMethod = "GET", notes = "列表查询Role", tags = "Role操作接口", response = JsonResult.class)
    public JsonResult findAll(@RequestParam Role queryPojo, int page, int size) {
        PageRequest pageRequest = PageRequest.of(page, size);
        Page<Role> data = roleService.findAll(queryPojo, pageRequest);

        return JsonResult.getInstance(data);
    }

    /**
     * 查询单个
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过主键查询Role", httpMethod = "GET", notes = "通过主键查询Role", tags = "Role操作接口", response = JsonResult.class)
    public JsonResult findById(@PathVariable("id") Long id) {
        Role data = roleService.findById(id).orElse(null);

        return JsonResult.getInstance(data);
    }

    /**
     * 删除
     */
    @DeleteMapping
    @ApiOperation(value = "通过主键删除Role", httpMethod = "DELETE", notes = "通过主键删除Role", tags = "Role操作接口", response = JsonResult.class)
    public JsonResult deleteById(@RequestBody Long id) {
        roleService.deleteById(id);

        return JsonResult.getInstance();
    }


}
